
package org.ceteca.explica.server.management.bl;

import java.math.BigDecimal;
import java.util.ArrayList;

import org.ceteca.explica.core.ExplicaException;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.management.DocumentLineOT;
import org.ceteca.explica.core.management.DocumentOT;
import org.ceteca.explica.core.management.MovementOT;
import org.ceteca.explica.core.management.SerialNumberOT;
import org.ceteca.explica.core.util.OperationResult;
import org.ceteca.explica.core.warehouse.LotOT;
import org.ceteca.explica.core.warehouse.ProductOT;
import org.ceteca.explica.core.warehouse.ProductStockOT;
import org.ceteca.explica.core.warehouse.StockOT;
import org.ceteca.explica.core.warehouse.WarehouseOT;
import org.ceteca.explica.server.common.dao.CommonDAO;
import org.ceteca.explica.server.util.db.CriteriaBean;
import org.ceteca.explica.server.util.db.InterfaceDB;

import com.sun.java_cup.internal.runtime.lr_parser;

/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class ManagementBLL {
	/** Unique instance of the ManagementBLL class. Singleton pattern */
	private static ManagementBLL instance;

	/** Returns the unique instance of the ManagementBLL class.
	 * Implements the singleton dessign pattern.
	 * @return ManagementBLL, unique instance of ManagementBLL.
	 */
	public static final ManagementBLL getInstance() {
		if (instance == null)
			instance = new ManagementBLL();
		return instance;
	}

	/**
	 * Default constructor.
	 */
	private ManagementBLL() {

	}

	public long nextSerialNumber(SerialNumberOT serial) throws ExplicaException{
	if (serial==null)
		return -1;
	long result=serial.getValue();
	CommonDAO dao= new CommonDAO();
	boolean existDocument=true;
	while(existDocument){
		ArrayList<CriteriaBean> lCriteria=new ArrayList<CriteriaBean>();
		lCriteria.add(new CriteriaBean("serialNumberPrefix", serial.getPrefix(), InterfaceDB.SQL_OPERATOR_EQUAL, null, null, null));
		lCriteria.add(new CriteriaBean("serialNumberValue", result, InterfaceDB.SQL_OPERATOR_EQUAL, InterfaceDB.LOGIC_OPERATOR_AND, null, null));
		ArrayList<DocumentOT> lDocuments=dao.getEntities(lCriteria, DocumentOT.class);
		if (lDocuments==null || lDocuments.isEmpty())
			existDocument=false;
		else
			result++;
	}
	serial.setValue(result+1);
	dao.saveEntity(serial);
	return result;
	}
	
	
	public OperationResult delEntities(ArrayList<CriteriaBean> lCriteria, Class myClass){
		CommonDAO dao= new CommonDAO();
		try {
			ArrayList<BaseVO> lMovements = dao.getEntities(lCriteria, myClass);
			ArrayList<Long> lIds = new ArrayList<Long>();
			for (BaseVO ent : lMovements) {
				((MovementOT)ent).getDocumentLine().getMovements().remove(ent);
				((MovementOT)ent).setDocumentLine(null);
				lIds.add(ent.getInternalId());
			}
			return dao.delEntities(lIds, myClass);
		}
		catch (Exception ex) {
			ex.printStackTrace();
			return null; 
		}
	}
	
	public ArrayList<StockOT> getProductStock(ProductOT product, WarehouseOT warehouse) throws ExplicaException 
	{
		
		ArrayList<StockOT> result;
		ArrayList<CriteriaBean> lCriteria=new ArrayList<CriteriaBean>();
		lCriteria.add(new CriteriaBean("product", product, InterfaceDB.SQL_OPERATOR_EQUAL, null, null, null));
		if (warehouse!=null)
		lCriteria.add(new CriteriaBean("warehouse", warehouse, InterfaceDB.SQL_OPERATOR_EQUAL, InterfaceDB.LOGIC_OPERATOR_AND, null, null));
		CommonDAO dao= new CommonDAO();
		
		
		result=dao.getEntities(lCriteria, ProductStockOT.class);
		
		
		return result;
		
	}
	
}